/*
		A todo list module for the desktop2 system, with items associated with a project.
		
		Author: 	Matthew Allpress
		Created:	12/06/06
		Edited:		24/09/06

*/

var todo_mod_name = "To Do List";

var ToDoBackend = 'http://localhost:5050/todo/index.php?mid=2'; // Backend url
var BackendBase = 'http://localhost/todo/index.php';

var toAdd = "<div style=\"position: absolute; overflow: scroll; overflow-x : hidden; width: 230px; height: 200px; z-index: 1; right: 0px; top: 0px; colour:white; background:#FFFFFF;\" id=\"todo\">\n</div>\n";

document.body.innerHTML+= toAdd;
toAdd = null;

var temp_element = document.getElementById("todo");
temp_element.style.top = module_config[todo_mod_name]['top'];
if(module_config[todo_mod_name]['left'] != "N")
{
	temp_element.style.left = module_config[todo_mod_name]['left'];
}
else
{
	temp_element.style.right = module_config[todo_mod_name]['right'];
}
temp_element = null;

window.setInterval("callUpdateToDo();", 60000);

callUpdateToDo();

function callUpdateToDo()
{
	makeNewRequest("GET", ToDoBackend, ToDoReqChange);
}


function display_window(op)
{
	objTemp = UpdateRequest();
	window.open(BackendBase+op, "","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,width=500,height=500");
}

/*
* onreadystatechange function
*/
function ToDoReqChange() 
{
 var content="";
 content= '<table border=\"1\" width=\"100%\">';
 // If data received correctly
 if (XMLRequestObject.readyState == 4) 
 {

  // if data is valid
  if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
  { 	
   // Parsing RSS
   try
   {
	   var node = XMLRequestObject.responseXML.documentElement; 
	   // Get Channel information
	   var projects = node.getElementsByTagName('project');
	   for (var i=0; i < projects.length; i++)
	   {
		//var project = projects[i].getElementsByTagName('project').item(0);
		var projID = projects[i].getElementsByTagName('id').item(0).firstChild.data;
		var name = projects[i].getElementsByTagName('name').item(0).firstChild.data;
		var descrip = projects[i].getElementsByTagName('descrip').item(0).firstChild.data;
		var status = projects[i].getElementsByTagName('status').item(0).firstChild.data;
		var projLink = '<input type=\"button\" name=\"show_project\" value=\">\" onclick=\"display_window(\'?op=proj&id='+projID+'\');\">';
		content+= '<tr>\n<td width=\"5%\">'+projLink+'</td>\n<td width=\"95%\"><b>'+name+'</b></td>\n</tr>';
		
		var items = projects[i].getElementsByTagName('item');
		if(items.length > 0)
		{
		 for (var n=0; n < items.length; n++)
		 { 
		  var itemName = items[n].getElementsByTagName('name').item(0).firstChild.data;
		  var itemDescrip = items[n].getElementsByTagName('descrip').item(0).firstChild.data;	
		  content += '<tr>\n<td colspan=\"2\">'+itemName+'</td>\n';
		 }
		}
	   }
	   // Display the result
	   //document.write(content);
		if(document.getElementById("todo") != content)
		{
			document.getElementById("todo").innerHTML = content;
		}
		node = null;
		projects = null;
		projID = null;
		name = null;
		descrip = null;
		status = null;
		projLink = null;
		contents = null;
	}
	catch(err)
	{
		add_to_console("Error returned when calling To Do list update, retrying");
		callUpdateToDo();
	}
  }
  else 
  {
   // Tell the reader that there was error requesting data
   alert("ERROR (ToDo Feed): There was an error requesting the data");
  }
 }
}

/*
* Main AJAX RSS reader request
*/

function display_window(op)
{
	window.open(BackendBase+op, "","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,width=500,height=500");
}
